package com.winai.launcher.view.guide;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.ks.projectbasictools.AppManager;
import com.ks.projectbasictools.retrofit.HttpResponseListener;
import com.ks.projectbasictools.utils.LogUtils;
import com.ks.projectbasictools.utils.ToastKs;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.base.db.DB_Common;
import com.winai.launcher.bean.responseEntity.DeviceImageEntity;
import com.winai.launcher.constant.Constant;
import com.winai.launcher.utils.PicassoLoadUtils;
import com.winai.launcher.utils.NotifyReceiverUtils;
import com.winai.launcher.utils.ServerUtils;
import com.winai.launcher.utils.cache.ProxyVideoCacheManager;
import com.winai.launcher.view.set.SetWifiLinkActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

import static com.winai.launcher.constant.HttpUrls.IMAGE_DEVICE;

/**
 * 作者：康少
 * 时间：2020/3/2 0002
 * 说明：引导完成
 */
public class GuideCompleteActivity extends BaseActivity {

    @BindView(R.id.iv_device)
    ImageView ivDevice;

    private Context mContext = this;
    private boolean canReplay = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_complete);
        ButterKnife.bind(this);
        getDeviceImage();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                canReplay = true;
            }
        }, 5000);
    }

    /**
     * 说明：获取设备外形图片
     */
    private void getDeviceImage() {
        if (!canIn()) {
            return;
        }
        ServerUtils.requestJsonPost(IMAGE_DEVICE, null, new Object(), new HttpResponseListener<DeviceImageEntity>() {
            @Override
            public void onResponse(DeviceImageEntity bean, boolean isCache) {
                String picturePath = bean.getData().getPicturePath();
                if (!TextUtils.isEmpty(picturePath)) {
                    PicassoLoadUtils.getInstance().picassoLoad(picturePath, ivDevice);
                } else {
                    PicassoLoadUtils.getInstance().picassoLoad(R.mipmap._image_loading, ivDevice);
                }
            }

            @Override
            public void onError(int code, String msg) {
                super.onError(code, msg);
                PicassoLoadUtils.getInstance().picassoLoad(R.mipmap._image_loading, ivDevice);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable e) {
                super.onFailure(call, e);
                PicassoLoadUtils.getInstance().picassoLoad(R.mipmap._image_loading, ivDevice);
            }
        });
    }

    private long sTime = 0;//请求的时间

    private boolean canIn() {
        //再次请求时间
        long ct = System.currentTimeMillis();
        if (ct - sTime < 2000) {
            LogUtils.d("2s内限制主屏轮播图接口请求");
            return false;
        }
        sTime = ct;
        return true;
    }

    /**
     * 说明：勿删！！
     *
     * @return true 放开上滑返回手势，默认true
     */
    public boolean isAllowCustomSliding() {
        return false;
    }

    private long rTime = 0;//请求的时间

    private boolean canRequeseReplay() {
        //再次请求时间
        long ct = System.currentTimeMillis();
        if (ct - rTime < 2000 || !canReplay) {
            LogUtils.d("2s内限制响应跳转按钮");
            return false;
        }
        rTime = ct;
        return true;
    }

    @OnClick({R.id.tv_replay, R.id.tv_start})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_replay:
                if (canRequeseReplay()) {
                    Intent intent = new Intent(this, GuideVideoActivity.class);
                    intent.putExtra("isReplay", true);
                    startActivity(intent);
                } else {
                    ToastKs.show(mContext, "点太快了，请慢一点");
                }
                break;
            case R.id.tv_start:
                DB_Common dbCommon = new DB_Common(mContext);
                dbCommon.add("isFirstOpenApp", "false");

                //清除引导页的视频缓存
                ProxyVideoCacheManager.clearAllCache(this);
                NotifyReceiverUtils.notifyPulldownReceiver(mContext, true);//允许下拉
                NotifyReceiverUtils.notifyToggleWakeUpReceiver(mContext, true);//允许唤醒
                NotifyReceiverUtils.notifyAiyishengLogin(mContext.getApplicationContext());//通知ai，艾益生登陆成功

                //模拟HOME
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(intent);

                AppManager.getAppManager().finishActivity(ConfigWifiActivity.class);
                AppManager.getAppManager().finishActivity(SetWifiLinkActivity.class);
                AppManager.getAppManager().finishActivity(GuideVideoActivity.class);
                finish();
                break;
        }
    }
}
